// 需要放在jquery.js、jquery.cookie.js下面

// 后台统一接口前缀
const api = 'http://47.114.172.10:30001'
// 前端统一接口前缀
const baseUrl = 'http://127.0.0.1:30000'
const loginUrl = baseUrl + '/login.html'

const TOKEN = 'WMS_TOKEN';
const PERMISSIONS = 'WMS_PERMISSIONS';
const USER_TYPE = 'WMS_USER_TYPE';
const USER_NAME = 'WMS_USER_NAME';

const CAPTCHA_EXPIRED = 419;
const FORM_ADD = 0;
const FORM_UPDATE = 1;
const SUCCESS_CODE = 200;
const OPERATION_TYPES = ['ADD','DELETE','UPDATE','QUERY'];
const MALE = 0;
const FEMALE = 1;

// 获取cookie中的登录TOKEN
var token = $.cookie(TOKEN);
//设置全局ajax前置拦截
$.ajaxSetup( { 
	headers: {
		'WMS_TOKEN': token   //每次ajax请求时把token带过去
	}
});

// 访问非登录页面时进行登录检验
if(window.location != loginUrl) {
  // 没有TOKEN，则直接跳转至登录页面
  if(token == undefined) {
    window.top.location=loginUrl;
  } else {
    // 有TOKEN，则实时检测登录状态
    $.ajax({
      url: api + "/login/checkLogin",
      method: "get",
      async: false,
      success:function(res){
        if(res.code != SUCCESS_CODE || !res.data) {
          window.top.location=loginUrl;
        }
      },
      error:function(){
        window.top.location=loginUrl;
      }
    })
  }
}

var pers=localStorage.getItem(PERMISSIONS);
var usertype=localStorage.getItem(USER_TYPE);
$(".btn-add").hide();
$(".btn-update").hide();
$(".btn-delete").hide();
$(".btn-dispatch").hide();
$(".btn-reset").hide();
if(usertype == 1){
	if(pers != 'null'){
		var permissions=pers.split(",");
		//部门权限开始
		if(permissions.indexOf("dept:add")<0){
			$(".btn-add-dept").hide();
		}
		if(permissions.indexOf("dept:update")<0){
			$(".btn-update-dept").hide();
		}
		if(permissions.indexOf("dept:delete")<0){
			$(".btn-delete-dept").hide();
		}
		//部门权限结束
	}
}

//给页面显示登录用户名
var username=localStorage.getItem(USER_NAME);
$(".login_name").html(username);

function generateUUID() {
  var d = new Date().getTime();
  if (window.performance && typeof window.performance.now === "function") {
      d += performance.now(); //use high-precision timer if available
  }
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      var r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}
